import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

var store = new Vuex.Store({
    state:{
        lunboArr:[],
        listArr:[],
        buyArr:[],
        userInfo:"",
        isLogin:false,
        username:"",
        avatar:"",
        oneInfo:"",
        isActive:1
    },
    mutations:{
        setLunbo:function(state,value){
            state.lunboArr = value;
        },
        setHarea:function(state,value){
            state.listArr = value;
        },
        setFastBuy:function(state,value){
            state.buyArr = value;
        },
        setUserInfo:function(state,value){
            state.userInfo = value;
        },
        setIsLogin:function(state,value){
            state.isLogin = value;
        },
        setUsername:function(state,value){
            state.username = value;
        },
        setAvatar:function(state,value){
            state.avatar = value;
        },
        setOneInfo:function(state,value){
            state.oneInfo = value;
        },
        setIsActive:function(state,value){
            state.isActive = value;
        }
    },
    actions:{
        requestLunbo:function(context,data){
            axios.get("/api/lunbo")
            .then(res=>{
                context.commit("setLunbo",res.data.msg);
            })
        },
        requestHarea:function(context,data){
            axios.get("/api/menu")
            .then(res=>{
                context.commit("setHarea",res.data.msg);
            })
        },
        requestFastBuy:function(context,data){
            axios.get("/api/fastbuy")
            .then(res=>{
                context.commit("setFastBuy",res.data.msg);
            })
        },
        requestRegist:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/regist",data)
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestLogin:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/login",data)
                .then(res=>{
                    context.commit("setIsLogin",res.data.isLogin);
                    resolve(res);
                })
            })
        },
        requestExit:function(context,data){
            return new Promise(function(resolve,reject){
                axios.get("/api/users/exit")
                .then(res=>{
                    context.commit("setIsLogin",res.data.isLogin);
                    resolve(res);
                })
            })
        },
        requestIsLogin:function(context,data){
            axios.get("/api/users/isLogin")
            .then(res=>{
                context.commit("setIsLogin",res.data.isLogin);
            })
        },
        requestUpload:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/upload",data,{
                    headers:{
                    "Content-Type":"multipart/form-data"
                    }
                })
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestUserInfo:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/userInfo")
                .then(res=>{
                    context.commit("setUsername",res.data.msg.username);
                    context.commit("setAvatar",res.data.msg.avatar[0]);
                })
            })
        },
        requestGoodUpload:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/goodUpload",data,{
                    headers:{
                    "Content-Type":"multipart/form-data"
                    }
                })
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestGoodList:function(context,data){
            return new Promise(function(resolve,reject){
                axios.get("/api/users/detail")
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestMine:function(context,data){
            return new Promise(function(resolve,reject){
                axios.get("/api/users/mine")
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestItem:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/item",data)
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestGoodSearch:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/goodSearch",data)
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestList:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/one",data)
                .then(res=>{
                    context.commit("setOneInfo",res.data.msg[0]);
                    resolve(res);
                })
            })
        },
        requestAdd:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/add",data)
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestMycart:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/mycart")
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestRemove:function(context,data){
            return new Promise(function(resolve,reject){
                axios.get("/api/users/remove",data)
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestDelete:function(context,data){
            return new Promise(function(resolve,reject){
                axios.get("/api/users/delete",data)
                .then(res=>{
                    resolve(res);
                })
            })
        },
        requestSort:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/sort",data)
                .then(res=>{
                    resolve(res);
                })
            })
        }
    }
})

export default store;